package cn.ctodb.divination.web.rest;

import com.codahale.metrics.annotation.Timed;
import cn.ctodb.divination.service.BihuaService;
import cn.ctodb.divination.web.rest.util.HeaderUtil;
import cn.ctodb.divination.service.dto.BihuaDTO;
import io.github.jhipster.web.util.ResponseUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

import static org.elasticsearch.index.query.QueryBuilders.*;

/**
 * REST controller for managing Bihua.
 */
@RestController
@RequestMapping("/api")
public class BihuaResource {

    private final Logger log = LoggerFactory.getLogger(BihuaResource.class);

    private static final String ENTITY_NAME = "bihua";
        
    private final BihuaService bihuaService;

    public BihuaResource(BihuaService bihuaService) {
        this.bihuaService = bihuaService;
    }

    /**
     * POST  /bihuas : Create a new bihua.
     *
     * @param bihuaDTO the bihuaDTO to create
     * @return the ResponseEntity with status 201 (Created) and with body the new bihuaDTO, or with status 400 (Bad Request) if the bihua has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/bihuas")
    @Timed
    public ResponseEntity<BihuaDTO> createBihua(@RequestBody BihuaDTO bihuaDTO) throws URISyntaxException {
        log.debug("REST request to save Bihua : {}", bihuaDTO);
        if (bihuaDTO.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new bihua cannot already have an ID")).body(null);
        }
        BihuaDTO result = bihuaService.save(bihuaDTO);
        return ResponseEntity.created(new URI("/api/bihuas/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /bihuas : Updates an existing bihua.
     *
     * @param bihuaDTO the bihuaDTO to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated bihuaDTO,
     * or with status 400 (Bad Request) if the bihuaDTO is not valid,
     * or with status 500 (Internal Server Error) if the bihuaDTO couldnt be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/bihuas")
    @Timed
    public ResponseEntity<BihuaDTO> updateBihua(@RequestBody BihuaDTO bihuaDTO) throws URISyntaxException {
        log.debug("REST request to update Bihua : {}", bihuaDTO);
        if (bihuaDTO.getId() == null) {
            return createBihua(bihuaDTO);
        }
        BihuaDTO result = bihuaService.save(bihuaDTO);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, bihuaDTO.getId().toString()))
            .body(result);
    }

    /**
     * GET  /bihuas : get all the bihuas.
     *
     * @return the ResponseEntity with status 200 (OK) and the list of bihuas in body
     */
    @GetMapping("/bihuas")
    @Timed
    public List<BihuaDTO> getAllBihuas() {
        log.debug("REST request to get all Bihuas");
        return bihuaService.findAll();
    }

    /**
     * GET  /bihuas/:id : get the "id" bihua.
     *
     * @param id the id of the bihuaDTO to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the bihuaDTO, or with status 404 (Not Found)
     */
    @GetMapping("/bihuas/{id}")
    @Timed
    public ResponseEntity<BihuaDTO> getBihua(@PathVariable Long id) {
        log.debug("REST request to get Bihua : {}", id);
        BihuaDTO bihuaDTO = bihuaService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(bihuaDTO));
    }

    /**
     * DELETE  /bihuas/:id : delete the "id" bihua.
     *
     * @param id the id of the bihuaDTO to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/bihuas/{id}")
    @Timed
    public ResponseEntity<Void> deleteBihua(@PathVariable Long id) {
        log.debug("REST request to delete Bihua : {}", id);
        bihuaService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }

    /**
     * SEARCH  /_search/bihuas?query=:query : search for the bihua corresponding
     * to the query.
     *
     * @param query the query of the bihua search 
     * @return the result of the search
     */
    @GetMapping("/_search/bihuas")
    @Timed
    public List<BihuaDTO> searchBihuas(@RequestParam String query) {
        log.debug("REST request to search Bihuas for query {}", query);
        return bihuaService.search(query);
    }


}
